<template>
  <div class="mapWrapPicture">
    <div class="containerWrap" ref="mapScreenfull">
      <div class="infoButton">
        <i @click='screenfullMap' class="iconfont icon-quanping2"></i>
      </div>
      <div class="colorWrapRack">
        <div v-for="(item,index) in colorslist" :key="index">
          <span v-show="item.color" class="colorlist" :style="'background-color:'+item.color"></span>
          <span v-show="item.src"><img src="./images/Intelligent.png" height="18" width="25"/></span>
          <span class="countStyle"> {{item.count}}</span>
          <span class="labelStyle">{{item.label}}</span>
        </div>
      </div>
      <div id="mapContainer"></div>
    </div>
  </div>
</template>

<script>
  export default {
    props: ["colorslist", "district", "areaInfo"],
    data () {
      return {
        map: "",
        searchInfoWindow: "",
        mapInfoWindow: "",
        markers: [],
        mapClickMarker: "",
        ply: [],

      }

    },
    mounted(){
      this.getHotMap()
    },
    created(){

    },
    methods: {
      //百度提取区域经纬度
      getBoundary(areaPoints) {
        let _this = this, arr = []
        if (areaPoints && areaPoints.length > 2) {
          _this.map.clearOverlays();        //清除地图覆盖物
          areaPoints.forEach((item) => {
            arr.push(new BMap.Point(item.lng, item.lat))
          })
          //              Polyline  Polygon
          _this.ply = new BMap.Polyline(arr, {
            strokeColor: "#3975FE",    //边线颜色。
            fillColor: "white",      //填充颜色。当参数为空时，圆形将没有填充效果。
            strokeWeight: 1,       //边线的宽度，以像素为单位。
            strokeOpacity: 1,    //边线透明度，取值范围0 - 1。
            fillOpacity: 1,      //填充的透明度，取值范围0 - 1。
            strokeStyle: 'solid' //边线的样式，solid或dashed。
          });  //创建多边形

          _this.map.addOverlay(_this.ply);  //添加覆盖物
          _this.map.setViewport(_this.ply.getPath());    //调整视野
          setTimeout(() => {
            _this.addMarkes(_this.areaInfo)
          }, 100)
        }
      },
      //清除指定覆盖物
      deletePoint(){
        let allOverlay = this.map.getOverlays();
        for (var i = 0; i < allOverlay.length - 1; i++) {
          if (allOverlay[i].getLabel().content == "我是id=1") {
            this.map.removeOverlay(allOverlay[i]);
            return false;
          }
        }
      },

      getHotMap(){
        let _this = this
        _this.map = new BMap.Map("mapContainer", {
          enableMapClick: false,
          minZoom: 10
        });// 创建地图实例

        /*_this.map.setMapStyle({
          styleJson: [
            {
              "featureType": "poilabel",
              "elementType": "all",
              "stylers": {
                "visibility": "off"
              }
            },
            {
              "featureType": "manmade",
              "elementType": "all",
              "stylers": {
                "visibility": "off"
              }
            },
            {
              "featureType": "building",
              "elementType": "all",
              "stylers": {
                "visibility": "off"
              }
            }
          ]
        });*/

        _this.map.centerAndZoom(new BMap.Point(this.$store.state.userInfo.lng, this.$store.state.userInfo.lat), 19);          // 初始化地图，设置中心点坐标和地图级别
        _this.map.enableScrollWheelZoom(); // 允许滚轮缩放

        _this.map.addControl(new BMap.OverviewMapControl());
        _this.map.addControl(new BMap.NavigationControl());  //添加默认缩放平移控件
        this.map.addControl( new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_RIGHT}));
//        地图点击事件
//        _this.addClickForMap()  //地图监听
        setTimeout(() => {
          _this.addMarkes(_this.areaInfo)
        }, 100)

      },

      addMarkes(dataInnfo){
        let _this = this, vectorMarker, arr = []
        dataInnfo.forEach((item2, index) => {
          if (item2.length > 0) {
            let color = this.colorslist[index].color
            item2.forEach(item1 => {
              /*if(item1.type==0){
                vectorMarker = new BMap.Marker(new BMap.Point(item1.lng, item1.lat), {
                  // 指定Marker的icon属性为Symbol
                  icon: new BMap.Icon(`./static/images/Intelligent.png`, new BMap.Size(34, 18)
                  )
                });
              }else */
              if (item1.areaPoints.length > 0) {
                let startIcon= new BMap.Marker(new BMap.Point(item1.lng, item1.lat), {icon: new BMap.Icon('./static/images/site' +(item1.type==0? 0 : item1.type==1 ? 4 : item1.type==2 ? 2 : 1)+ '.png',new BMap.Size(32, 70)),})
                _this.map.addOverlay(startIcon)
                _this.addClickHandler(startIcon, item1);
                arr = []
                item1.areaPoints.forEach(item => {
                  arr.push(new BMap.Point(item.lng, item.lat))
                })
                vectorMarker = new BMap.Polygon(arr, {
                  strokeColor: color,    //边线颜色。
                  fillColor: color,      //填充颜色。当参数为空时，圆形将没有填充效果。
                  strokeWeight: 1,       //边线的宽度，以像素为单位。
                  strokeOpacity: 0.9,    //边线透明度，取值范围0 - 1。
                  fillOpacity: 0.2,      //填充的透明度，取值范围0 - 1。
                  strokeStyle: 'solid' //边线的样式，solid或dashed。
                });//创建多边形
              }
              _this.map.addOverlay(vectorMarker);
              // 点击事件
              _this.addClickHandler(vectorMarker, item1);
            })
          }
        })
      },
      addClickForMap(){
        let _this = this
        this.map.addEventListener("click", function (e) {
          if(this.$store.state.btnMenuIds.indexOf(37)==-1){
            return
          }
          if (!e.overlay || e.overlay && e.overlay.toString() == "[object Polyline]") {  //您点击的是地图或者线
            let point = e.point, regionName = "", searchInfo = {}, streetId = ""
            _this.axios.post(process.env.API_HOST + `/common/selectRegionByPoint`, {lat: point.lat, lng: point.lng})
              .then(response => {
                const result = response.data
                if (result.code == 200) {
                  if (_this.district && result.result.child) { //有行政区 查询街道
                    regionName = result.result.regionName + result.result.child.regionName
                    streetId = result.result.child.id
                    if (result.result.child.regionPoints) {
                      _this.getBoundary(JSON.parse(result.result.child.regionPoints))
                    }
                  } else {  //无行政区 查询区域
                    regionName = result.result.regionName
                    if (result.result.regionPoints) {
                      _this.getBoundary(JSON.parse(result.result.regionPoints))
                    }
                  }

                  _this.axios.post(process.env.API_HOST + `/facilities/selectAreaCount`, {
                    district: result.result.id,
                    street: streetId
                  })
                    .then(data => {
                      const info = data.data
                      if (info.code == 200) {
                        searchInfo = {regionName: regionName, ...info.result}
                        if (searchInfo) {
                          _this.clickMapSearch(point, searchInfo)
                        }
                      }
                    })
                }
              })

          }

        }, false)

      },
      clickMapSearch(point, info){
        let content, name = info.regionName, _this = this
        //创建检索信息窗口对象
        _this.mapInfoWindow = null;

        content = '<div class="mapTopWrap" > <div class="mapBaseTitle" style="">设施信息</div> <div style="float: left;line-height: 31px;border-left: 1px solid #EFEEF3; "> <div style="padding: 0 10px;color: #50545D;">电子围栏：' + info.railCount + '</div> <div style="padding: 0 10px;color: #50545D; display:none;">智能站点：' + info.siteCount + '</div> <div style="padding: 0 10px;color: #50545D;">自行车道：(待定)</div> </div> </div> '

        _this.mapInfoWindow = new BMapLib.SearchInfoWindow(_this.map, content, {
          title: name,      //标题
          panel: "panel",         //检索结果面板
          enableAutoPan: true,     //自动平移
          offset: new BMap.Size(-20, -20),
          searchTypes: []
        });
        _this.mapClickMarker = new BMap.Marker(point); //创建marker对象
        _this.mapInfoWindow.open(_this.mapClickMarker)

      },
      addClickHandler (marker, info) {

        let _this = this
        marker.addEventListener("click", function (e) {
            let point = e.point
            let content, name = '设施信息', areaType = info.type == 0 ? '停放区' : info.type==1 ? '监控区' : info.type==2 ? '电子围栏' : '禁停区'

            //创建检索信息窗口对象
            this.searchInfoWindow = null;
            if(_this.IEVersion()==-1){
                content = '<div class="mapTopWrap"><div style="color: #50545D; padding: 10px;">类型：' + areaType + '</div></div> '
            }else {
                content = '<div class="mapTopWrap"><div style="color: #50545D; padding: 10px;">类型：' + areaType + '</div></div> '
            }

            this.searchInfoWindow = new BMapLib.SearchInfoWindow(this.map, content, {
              title: name,      //标题
              panel: 'panel',         //检索结果面板
              enableAutoPan: true,     //自动平移
              offset: new BMap.Size(-20, -20),
              searchTypes: []
            });
            this.marker = new BMap.Marker(point); //创建marker对象
            this.searchInfoWindow.open(this.marker)
          }, true
        )
      },
      screenfullMap(){
        if (this.screenfull.enabled) {
          this.screenfull.toggle(this.$refs.mapScreenfull);
        }
      },

    },
    watch: {
      areaInfo(){
        this.getHotMap()
      },

    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "../../../common/stylus/mixin.styl"
  .mapWrapPicture
    width: 100%;
    height 100%
    &:hover .infoButton
      opacity: 1 !important;
    .containerWrap
      height: 100%;
      width: 100%;
      margin 0px auto;
      position: relative;
      .infoButton
        height 31px
        font-size: 16px;
        color: #6A6FFC;
        position: absolute;
        top: 10px;
        right: 10px;
        z-index 5
        opacity: 0;
        .iconfont
          display: inline-block;
          font-size: 18px;
          cursor pointer
      .colorWrapRack
        position: absolute;
        left 10px;
        top: 10px;
        line-height 25px
        vertical-align middle
        padding 10px
        border-radius 5px
        z-index: 5;
        color: black;
        background-color rgba(255, 255, 255, 0.8)
        .colorlist
          display: inline-block;
          width: 21px;
          height 13px;
          line-height: 25px;
          vertical-align: middle;
        .labelStyle
          font-size: 12px;
          color: #50545D;
          float: right;
          margin-left: 10px;
        .countStyle
          color: #3975FE;
          float: right;
          min-width: 40px;
          text-align: right;
      #mapContainer
        height: 100%;
        width: 100%;
    .mapTopWrap
      background-color: white;
      overflow: hidden;
      // min-height 90px;
      /*width 240px*/
      .mapBaseTitle
        color: #3975FE;
        font-size: 12px;
        writing-mode: vertical-lr;
        letter-spacing: 5px;
        float: left;
        padding: 11px;

    .BMapLib_SearchInfoWindow
      width 200px !important
      &::after
        content: ""
        position: absolute;
        background-color: white;
        width: 100px;
        height: 3px;
        bottom: 0px;
        left: 50px;
        z-index 100

    .BMapLib_bubble_title {
      padding-left: 14px !important;
      font-size: 14px !important;
      font-weight: bold !important;
      background-color white
      border-radius 10px !important
      /*display none !important*/
    }

    .BMapLib_bubble_top, .BMapLib_bubble_close {
      border-radius 10px !important
      border none !important
      background-color #f9f9f9 !important
    }

    #BMapLib_SearchInfoWindow0, .BMapLib_SearchInfoWindow {
      border-radius 10px !important
      /*min-width 200px !important*/
      box-shadow: 0px 0px 24px 0px rgba(0, 0, 0, 0.34) !important
      border none
    }

    .BMapLib_SearchInfoWindow .BMapLib_bubble_center {
      position static !important /*position absolute
      left 0
      top 31px*/
    }

    /*.BMapLib_bubble_content {
      min-height 100px !important
    }*/

</style>
